package com.bckj.fastboot.mq.ons.retry;

import com.aliyun.openservices.ons.api.Message;
import com.aliyun.openservices.ons.api.Producer;
import com.aliyun.openservices.ons.api.SendResult;
import com.bckj.fastboot.core.extra.retry.RetryContext;
import com.bckj.fastboot.core.extra.retry.RetryProducer;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;

@Slf4j
@RequiredArgsConstructor
public class OnsRetryProducer implements RetryProducer {

    private final Producer producer;
    @Value("${fastboot.ons.config.retryTopic}")
    private String retryTopic;

    /**
     * 发送延时重试消息
     * @param retryContext  重试消息
     * @param delaySeconds  延时秒数（可自由定义，如 10/30/60）
     */
    @Override
    public void sendRetryMessage(RetryContext retryContext, long delaySeconds) {
        Message msg = new Message(
                retryTopic,
                "RETRY_TAG",
                retryContext.getBizId(),
                retryContext.toJson().getBytes()
        );
        msg.setStartDeliverTime(System.currentTimeMillis() + delaySeconds * 1000); // 关键：设置延时时间
        SendResult sendResult = producer.send(msg);
        log.info("重试消息已发送: " + sendResult.getMessageId());
    }
}
